package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dao.ClassesDao;
import com.example.data.request.ReqClassesList;
import com.example.data.response.RespClassesInfo;
import com.example.entity.Classes;
import com.example.service.ClassesService;
import com.example.utils.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Author:yanghua
 * Date:2023/10/7 10:05
 */
@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesDao, Classes> implements ClassesService {
    @Resource
    private ClassesDao classesDao;

    @Override
    public R addOneClass(Classes classes) {
        List<Classes> allClasses = classesDao.getAllClasses();
        Integer grade = classes.getGrade();
        Integer className = classes.getClassName();
        for (Classes c : allClasses) {
            if (Objects.equals(c.getGrade(), grade) && Objects.equals(c.getClassName(), className)) {
                return R.error("创建的班级重复了！");
            }
        }
        int i = classesDao.insert(classes);
        Map<String, Object> data = new HashMap<>();
        data.put("info", classes);
        return i == 1 ? R.ok(data, "添加班级信息成功") : R.error("添加班级信息失败");
    }

    @Override
    public R getClassesList(ReqClassesList reqClassesList) {
        reqClassesList.setPage((reqClassesList.getPage() - 1) * reqClassesList.getSize());
        List<RespClassesInfo> classesList = classesDao.getClassesList(reqClassesList);
        Integer number = classesDao.getClassesNumber(reqClassesList);
        Map<String, Object> data = new HashMap<>();
        data.put("list", classesList);
        data.put("size", number);
        return R.ok(data, "查询成功");
    }

    @Override
    public R deleteClassById(Long id) {
        Integer integer = classesDao.deleteClassById(id);
        return integer == 1 ? R.ok("删除成功") : R.error("删除失败");
    }

    @Override
    public R updateInfo(Classes classes) {
        int i = classesDao.updateById(classes);
        return i == 1 ? R.ok("修改成功") : R.error("修改失败");
    }
}
